﻿/*
 *  ChgDoc CMS - GNU General Public License, version 3 (GPL-3.0)
 *  Copyright (C)  2012. Ian Quigley
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Routing;
using System.Reflection;
using ChgDoc.Entity;

namespace ChgDoc
{
    public class Global : System.Web.HttpApplication
    {
        void RegisterRoutes(RouteCollection routes)
        {
            routes.Clear();
            routes.MapHttpHandler<ResponseHandler>( "" );
            routes.MapHttpHandler<ResponseHandler>( @"{url}" );
            routes.MapHttpHandler<ResponseHandler>( @"{url}/{first}" );
            routes.MapHttpHandler<ResponseHandler>( @"{url}/{first}/{second}" );
            routes.MapHttpHandler<ResponseHandler>( @"{url}/{first}/{second}/{third}" );
        }

        protected void Application_Start(object sender, EventArgs e)
        {
            
            RegisterRoutes(RouteTable.Routes);         
            ChgDoc.Config.Initialize(Assembly.GetExecutingAssembly());            
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            ChgDoc.Config.Log.Info("Session started");       
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
#if !DEBUG
            Exception ex = Server.GetLastError();
            Server.ClearError();

            ChgDoc.Config.Log.Error("** Exception", ex);
           
            HttpContext.Current.Response.Clear();
            if (Visitor.HasPermission( "Admin" ))
            {
                HttpContext.Current.Response.Write( "<html>" );
                HttpContext.Current.Response.Write( "Exception<br/>" );
                HttpContext.Current.Response.Write( ex.Message  + "<br/>");
                HttpContext.Current.Response.Write( ex.StackTrace + "<br/>" );
                HttpContext.Current.Response.Write( "</html>" );
            }
            else
            {             
                HttpContext.Current.Response.Write( "Unknown error occured" );
            }

#endif       
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }


    public static class HttpHandlerExtensions
    {
        public static void MapHttpHandler<THandler>(this RouteCollection routes,
          string url) where THandler : IHttpHandler, new()
        {
            routes.MapHttpHandler<THandler>(null, url, null, null);
        }

        public static void MapHttpHandler<THandler>(this RouteCollection routes,
            string name, string url, object defaults, object constraints)
            where THandler : IHttpHandler, new()
        {
            var route = new Route(url, new HttpHandlerRouteHandler<THandler>());
            route.Defaults = new RouteValueDictionary(defaults);
            route.Constraints = new RouteValueDictionary(constraints);
            routes.Add(name, route);
        }
    }

}